<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: History + TabView</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: History + TabView</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
<p>
This example demonstrates how to add browser history support to a TabView widget
using the History Utility.
</p>

<p>
Select a new tab in the TabView below, then use your browser's back button
to return to the previously selected tab. Next, click on one of the images to
visit the Flickr photo page for that image, then use your browser's back button
to return to the current page with the same tab selected.
</p>
</div>

<div class="example">
    <style scoped>
        #demo { width: 300px; }

        #demo img {
            border: 1px solid #000;
            height: 150px;
        }

        #demo .yui3-tab-selected .yui3-tab-label { color: #fff; }
    </style>

    <div id="demo" class="yui3-skin-sam">
  <ul>
    <li><a href="#asparagus">Asparagus</a></li>
    <li><a href="#bird">Bird</a></li>
    <li><a href="#coffee">Coffee</a></li>
  </ul>
  <div>
    <div id="asparagus">
      <a href="http://www.flickr.com/photos/allenr/4686935131/">
        <img src="http://farm5.static.flickr.com/4005/4686935131_253e921bf7_m.jpg" alt="Asparagus">
      </a>
    </div>
    <div id="bird">
      <a href="http://www.flickr.com/photos/allenr/66307916/">
        <img src="http://farm1.static.flickr.com/26/66307916_811efccdfc_m.jpg" alt="Bird">
      </a>
    </div>
    <div id="coffee">
      <a href="http://www.flickr.com/photos/allenr/4638474362/">
        <img src="http://farm4.static.flickr.com/3336/4638474362_093edb7565_m.jpg" alt="Coffee">
      </a>
    </div>
  </div>
</div>

<script>
YUI().use('history', 'tabview', function (Y) {
  var history = new Y.HistoryHash(),
      tabview = new Y.TabView({srcNode: '#demo'});

  // Render the TabView widget to turn the static markup into an
  // interactive TabView.
  tabview.render();

  // Set the selected tab to the bookmarked history state, or to
  // the first tab if there's no bookmarked state.
  tabview.selectChild(history.get('tab') || 0);

  // Store a new history state when the user selects a tab.
  tabview.after('selectionChange', function (e) {
    // If the new tab index is greater than 0, set the "tab"
    // state value to the index. Otherwise, remove the "tab"
    // state value by setting it to null (this reverts to the
    // default state of selecting the first tab).
    history.addValue('tab', e.newVal.get('index') || null);
  });

  // Listen for history changes from back/forward navigation or
  // URL changes, and update the tab selection when necessary.
  Y.on('history:change', function (e) {
    // Ignore changes we make ourselves, since we don't need
    // to update the selection state for those. We're only
    // interested in outside changes, such as the ones generated
    // when the user clicks the browser's back or forward buttons.
    if (e.src === Y.HistoryHash.SRC_HASH) {

      if (e.changed.tab) {
        // The new state contains a different tab selection, so
        // change the selected tab.
        tabview.selectChild(e.changed.tab.newVal);
      } else if (e.removed.tab) {
        // The tab selection was removed in the new state, so
        // select the first tab by default.
        tabview.selectChild(0);
      }

    }
  });
});
</script>

</div>

<h2>HTML</h2>

<p>
First, create the markup for a simple TabView widget with three tabs.
</p>

<pre class="code prettyprint">&lt;div id=&quot;demo&quot; class=&quot;yui3-skin-sam&quot;&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#asparagus&quot;&gt;Asparagus&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
    &lt;li&gt;&lt;a href=&quot;#bird&quot;&gt;Bird&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
    &lt;li&gt;&lt;a href=&quot;#coffee&quot;&gt;Coffee&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
  &lt;&#x2F;ul&gt;
  &lt;div&gt;
    &lt;div id=&quot;asparagus&quot;&gt;
      &lt;a href=&quot;http:&#x2F;&#x2F;www.flickr.com&#x2F;photos&#x2F;allenr&#x2F;4686935131&#x2F;&quot;&gt;
        &lt;img src=&quot;http:&#x2F;&#x2F;farm5.static.flickr.com&#x2F;4005&#x2F;4686935131_253e921bf7_m.jpg&quot; alt=&quot;Asparagus&quot;&gt;
      &lt;&#x2F;a&gt;
    &lt;&#x2F;div&gt;
    &lt;div id=&quot;bird&quot;&gt;
      &lt;a href=&quot;http:&#x2F;&#x2F;www.flickr.com&#x2F;photos&#x2F;allenr&#x2F;66307916&#x2F;&quot;&gt;
        &lt;img src=&quot;http:&#x2F;&#x2F;farm1.static.flickr.com&#x2F;26&#x2F;66307916_811efccdfc_m.jpg&quot; alt=&quot;Bird&quot;&gt;
      &lt;&#x2F;a&gt;
    &lt;&#x2F;div&gt;
    &lt;div id=&quot;coffee&quot;&gt;
      &lt;a href=&quot;http:&#x2F;&#x2F;www.flickr.com&#x2F;photos&#x2F;allenr&#x2F;4638474362&#x2F;&quot;&gt;
        &lt;img src=&quot;http:&#x2F;&#x2F;farm4.static.flickr.com&#x2F;3336&#x2F;4638474362_093edb7565_m.jpg&quot; alt=&quot;Coffee&quot;&gt;
      &lt;&#x2F;a&gt;
    &lt;&#x2F;div&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;</pre>


<h2>JavaScript</h2>

<h3>Load history and tabview</h3>

<p>
Load the <code>history</code> and <code>tabview</code> modules.
</p>

<pre class="code prettyprint">YUI().use(&#x27;history&#x27;, &#x27;tabview&#x27;, function (Y) {
  &#x2F;&#x2F; ...implementation code...
});</pre>


<h3>Initialize History and TabView</h3>

<p>
Create an instance of the TabView widget and a history adapter, and restore the
bookmarked tab selection, if any.
</p>

<p>
This example uses the <code>Y.HistoryHash</code> adapter, which stores history state in the
hash fragment of the URL. Another option would be to use the <code>Y.HistoryHTML5</code>
adapter, but this would require additional logic in order to create bookmarkable
URLs.
</p>

<p>
Add this code inside the <code>YUI().use()</code> callback from the previous step.
</p>

<pre class="code prettyprint">var history = new Y.HistoryHash(),
    tabview = new Y.TabView({srcNode: &#x27;#demo&#x27;});

&#x2F;&#x2F; Render the TabView widget to turn the static markup into an
&#x2F;&#x2F; interactive TabView.
tabview.render();

&#x2F;&#x2F; Set the selected tab to the bookmarked history state, or to
&#x2F;&#x2F; the first tab if there&#x27;s no bookmarked state.
tabview.selectChild(history.get(&#x27;tab&#x27;) || 0);</pre>


<h3>Add a history entry when the selected tab changes</h3>

<p>
When the user selects a new tab, create a new browser history entry with a
state value named <code>tab</code> that contains the index of the selected tab.
If the first tab is selected, then set the <code>tab</code> state value to
<code>null</code> to remove it from the state, because the first tab is the
default tab.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Store a new history state when the user selects a tab.
tabview.after(&#x27;selectionChange&#x27;, function (e) {
  &#x2F;&#x2F; If the new tab index is greater than 0, set the &quot;tab&quot;
  &#x2F;&#x2F; state value to the index. Otherwise, remove the &quot;tab&quot;
  &#x2F;&#x2F; state value by setting it to null (this reverts to the
  &#x2F;&#x2F; default state of selecting the first tab).
  history.addValue(&#x27;tab&#x27;, e.newVal.get(&#x27;index&#x27;) || null);
});</pre>


<h3>Listen for history events to capture back/forward navigation</h3>

<p>
Finally, listen for history change events, which indicate that the user clicked
the back/forward button or manually changed the URL.
</p>

<p>
When a <code>history:change</code> event occurs, it could come from the call to
the <code>addValue()</code> method above or it could come from a change to the
URL hash. We only care about changes that come from the URL hash, since that
indicates a navigation event.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Listen for history changes from back&#x2F;forward navigation or
&#x2F;&#x2F; URL changes, and update the tab selection when necessary.
Y.on(&#x27;history:change&#x27;, function (e) {
  &#x2F;&#x2F; Ignore changes we make ourselves, since we don&#x27;t need
  &#x2F;&#x2F; to update the selection state for those. We&#x27;re only
  &#x2F;&#x2F; interested in outside changes, such as the ones generated
  &#x2F;&#x2F; when the user clicks the browser&#x27;s back or forward buttons.
  if (e.src === Y.HistoryHash.SRC_HASH) {

    if (e.changed.tab) {
      &#x2F;&#x2F; The new state contains a different tab selection, so
      &#x2F;&#x2F; change the selected tab.
      tabview.selectChild(e.changed.tab.newVal);
    } else if (e.removed.tab) {
      &#x2F;&#x2F; The tab selection was removed in the new state, so
      &#x2F;&#x2F; select the first tab by default.
      tabview.selectChild(0);
    }

  }
});</pre>


<h3>Complete example source code</h3>

<p>
Here's the complete source code for the example:
</p>

<pre class="code prettyprint">&lt;div id=&quot;demo&quot; class=&quot;yui3-skin-sam&quot;&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#asparagus&quot;&gt;Asparagus&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
    &lt;li&gt;&lt;a href=&quot;#bird&quot;&gt;Bird&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
    &lt;li&gt;&lt;a href=&quot;#coffee&quot;&gt;Coffee&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
  &lt;&#x2F;ul&gt;
  &lt;div&gt;
    &lt;div id=&quot;asparagus&quot;&gt;
      &lt;a href=&quot;http:&#x2F;&#x2F;www.flickr.com&#x2F;photos&#x2F;allenr&#x2F;4686935131&#x2F;&quot;&gt;
        &lt;img src=&quot;http:&#x2F;&#x2F;farm5.static.flickr.com&#x2F;4005&#x2F;4686935131_253e921bf7_m.jpg&quot; alt=&quot;Asparagus&quot;&gt;
      &lt;&#x2F;a&gt;
    &lt;&#x2F;div&gt;
    &lt;div id=&quot;bird&quot;&gt;
      &lt;a href=&quot;http:&#x2F;&#x2F;www.flickr.com&#x2F;photos&#x2F;allenr&#x2F;66307916&#x2F;&quot;&gt;
        &lt;img src=&quot;http:&#x2F;&#x2F;farm1.static.flickr.com&#x2F;26&#x2F;66307916_811efccdfc_m.jpg&quot; alt=&quot;Bird&quot;&gt;
      &lt;&#x2F;a&gt;
    &lt;&#x2F;div&gt;
    &lt;div id=&quot;coffee&quot;&gt;
      &lt;a href=&quot;http:&#x2F;&#x2F;www.flickr.com&#x2F;photos&#x2F;allenr&#x2F;4638474362&#x2F;&quot;&gt;
        &lt;img src=&quot;http:&#x2F;&#x2F;farm4.static.flickr.com&#x2F;3336&#x2F;4638474362_093edb7565_m.jpg&quot; alt=&quot;Coffee&quot;&gt;
      &lt;&#x2F;a&gt;
    &lt;&#x2F;div&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;

&lt;script&gt;
YUI().use(&#x27;history&#x27;, &#x27;tabview&#x27;, function (Y) {
  var history = new Y.HistoryHash(),
      tabview = new Y.TabView({srcNode: &#x27;#demo&#x27;});

  &#x2F;&#x2F; Render the TabView widget to turn the static markup into an
  &#x2F;&#x2F; interactive TabView.
  tabview.render();

  &#x2F;&#x2F; Set the selected tab to the bookmarked history state, or to
  &#x2F;&#x2F; the first tab if there&#x27;s no bookmarked state.
  tabview.selectChild(history.get(&#x27;tab&#x27;) || 0);

  &#x2F;&#x2F; Store a new history state when the user selects a tab.
  tabview.after(&#x27;selectionChange&#x27;, function (e) {
    &#x2F;&#x2F; If the new tab index is greater than 0, set the &quot;tab&quot;
    &#x2F;&#x2F; state value to the index. Otherwise, remove the &quot;tab&quot;
    &#x2F;&#x2F; state value by setting it to null (this reverts to the
    &#x2F;&#x2F; default state of selecting the first tab).
    history.addValue(&#x27;tab&#x27;, e.newVal.get(&#x27;index&#x27;) || null);
  });

  &#x2F;&#x2F; Listen for history changes from back&#x2F;forward navigation or
  &#x2F;&#x2F; URL changes, and update the tab selection when necessary.
  Y.on(&#x27;history:change&#x27;, function (e) {
    &#x2F;&#x2F; Ignore changes we make ourselves, since we don&#x27;t need
    &#x2F;&#x2F; to update the selection state for those. We&#x27;re only
    &#x2F;&#x2F; interested in outside changes, such as the ones generated
    &#x2F;&#x2F; when the user clicks the browser&#x27;s back or forward buttons.
    if (e.src === Y.HistoryHash.SRC_HASH) {

      if (e.changed.tab) {
        &#x2F;&#x2F; The new state contains a different tab selection, so
        &#x2F;&#x2F; change the selected tab.
        tabview.selectChild(e.changed.tab.newVal);
      } else if (e.removed.tab) {
        &#x2F;&#x2F; The tab selection was removed in the new state, so
        &#x2F;&#x2F; select the first tab by default.
        tabview.selectChild(0);
      }

    }
  });
});
&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Demonstrates how to add browser history support to a TabView widget using the History Utility.">
                                        <a href="history-tabview.html">History + TabView</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
